package common.dao.Impl;

import cn.hutool.core.util.StrUtil;
import common.dao.StudentDao;
import common.domain.Student;
import utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class StudentDaoImpl implements StudentDao {
    private Connection con;
    private PreparedStatement psmt;
    private ResultSet rs;
    @Override
    public void insert(Student student) {
        String sql = "insert into student\n" +
                "        (student_type,student_name,user_id,major_id,teacher_id) \n" +
                "values(?,?,?,?,?)";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setInt(1,student.getStudentType());
            psmt.setString(2,student.getStudentName());
            psmt.setLong(3,student.getUserId());
            psmt.setLong(4,student.getMajorId());
            psmt.setLong(5,student.getTeacherId());
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public void updateById(Student student) {
        String sql = "update student set";
        List<Object> param = new ArrayList<>();
        if (student == null){
            return;
        }else{
            if(student.getStudentId() != null){
                sql += " student_id = ?,";
                param.add(student.getStudentId());
            }
            if(student.getStudentType() != null){
                sql += " student_type = ?,";
                param.add(student.getStudentType());
            }
            if(student.getStudentName() != null){
                sql += " student_name = ?,";
                param.add(student.getStudentName());
            }
            if(student.getUserId() != null){
                sql += " user_id = ?,";
                param.add(student.getUserId());
            }
            if(student.getMajorId() != null){
                sql += " major_id = ?,";
                param.add(student.getMajorId());
            }
            if(student.getTeacherId() != null){
                sql += " teacher_id = ?,";
                param.add(student.getTeacherId());
            }
        }
        sql = StrUtil.strip(sql,null,",");
        sql+=" where student_id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for(int i = 0; i < param.size(); i++){
                psmt.setObject(i+1,param.get(i));
            }
            psmt.setLong(param.size()+1,student.getStudentId());
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public void deleteById(Long id) {
        String sql = "delete from student where student_id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,id);
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public Student selectById(Long id) {
        String sql = "select * from student where student_id = ?";
        Student student = new Student();
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,id);
            rs = psmt.executeQuery();
            while (rs.next()){
                student.setStudentId(rs.getLong(1));
                student.setStudentType(rs.getInt(2));
                student.setStudentName(rs.getString(3));
                student.setUserId(rs.getLong(4));
                student.setMajorId(rs.getLong(5));
                student.setTeacherId(rs.getLong(6));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt,rs);
        }
        return student;
    }

    @Override
    public List<Student> selectList(Student student) {
        String sql = "select * from student where 1=1";
        List<Student> list = new ArrayList<>();
        List<Object> param = new ArrayList<>();
        if (student == null){
            sql = "select * from student";
        }else{
            if(student.getStudentId() != null){
                sql += " and student_id = ?";
                param.add(student.getStudentId());
            }
            if(student.getStudentType() != null){
                sql += " and student_type = ?";
                param.add(student.getStudentType());
            }
            if(student.getStudentName() != null){
                sql += " and student_name like \"%\"?\"%\"";
                param.add(student.getStudentName());
            }
            if(student.getUserId() != null){
                sql += " and user_id = ?";
                param.add(student.getUserId());
            }
            if(student.getMajorId() != null){
                sql += " and major_id = ?";
                param.add(student.getMajorId());
            }
            if(student.getTeacherId() != null){
                sql += " and teacher_id = ?";
                param.add(student.getTeacherId());
            }
        }
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for(int i = 0; i < param.size(); i++){
                psmt.setObject(i+1,param.get(i));
            }
            rs = psmt.executeQuery();
            while (rs.next()){
                Student student1 = new Student();
                student1.setStudentId(rs.getLong(1));
                student1.setStudentType(rs.getInt(2));
                student1.setStudentName(rs.getString(3));
                student1.setUserId(rs.getLong(4));
                student1.setMajorId(rs.getLong(5));
                student1.setTeacherId(rs.getLong(6));
                list.add(student1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt,rs);
        }
        return list ;
    }
}
